//
//  VisitorView.swift
//  sinaWeibo
//
//  Created by 史文杰 on 16/3/7.
//  Copyright © 2016年 swjie87. All rights reserved.
//

import UIKit
import SnapKit

//MARK: 声明协议
@objc protocol VisitorViewDelegate :NSObjectProtocol {
    //协议方法
   optional func userWillLogin()
   optional func userWillRegisterBtn()
    
}


class VisitorView: UIView {
    
    //var userLoginBtnClick: (()->())?

    //定义代理属性
    weak var visitorDelegate: VisitorViewDelegate?
    
    //MARK: 设置访客视图
    func setupInfoWith(title: String , imageName: String?)
    {
        tipLabel.text = title
        //判断是不是主页 下面在懒加载的时候 已经默认给主页的访问页设置属性了
        if imageName != nil {
            //不是主页
            circleIcon.image = UIImage(named: imageName!)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }
        else
        {   //是主页 开启动画效果
            homeCircleIconAnimation()
        }
    }
    
    //MARK: 旋转动画
    private func homeCircleIconAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //每次执行的时间
        animation.duration = 10
        //动画次数
        animation.repeatCount = MAXFLOAT
        //
        animation.toValue = 2 * M_PI
        //结束后是否移除
        animation.removedOnCompletion = false
        //添加动画
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
    
    
    //先走此方法 然后在 执行 override init(frame: CGRect)
    //    init() {
    //
    //        super.init(frame:CGRectZero)
    //    }
    
    //此方法会创建 View 要在此方法内添加子视图
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        setUpView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //MARK: 给View添加子视图
    private func setUpView()
    {
        //添加子视图
        addSubview(circleIcon)
        addSubview(maskImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-100)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            //make.centerY.equalTo(circleIcon.snp_centerY).offset(120)
            make.top.equalTo(circleIcon.snp_bottom).offset(15)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(tipLabel.snp_leading)
            make.top.equalTo(tipLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(largeHouse.snp_center)
        }
        let value: CGFloat = 236.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        //设置按钮的监听方法
        loginBtn.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
    }
    
    //MARK: private 使方法私有，但是如果不加 @obj 会崩溃
    @objc private func loginBtnClick() {
        //闭包方式
       //userLoginBtnClick!()
        
        
        visitorDelegate?.userWillLogin?()
    }
    @objc private func registerBtnClick() {
        
        visitorDelegate?.userWillRegisterBtn?()
        
    }
    
    
    //MARK:懒加载子视图
    //圆形图
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文字
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.orangeColor()
        l.font = UIFont.systemFontOfSize(14)
        l.sizeToFit()
        l.numberOfLines = 0
        l.textAlignment = .Center
        
        return l
    }()
    
    //登陆与注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        
        //根据图片的大小设置切图的范围
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width*0.5)
        let capH = Int(image.size.height*0.5)
        //设置背景图并切图
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH
            ), forState: .Normal)
        
        //        btn.setBackgroundImage(UIImage(named:"common_button_white_disable" ), forState: .Normal)
        btn.setTitle("登陆", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.sizeToFit()
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //根据图片的大小设置切图的范围
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width*0.5)
        let capH = Int(image.size.height*0.5)
        //设置背景图并切图
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH
            ), forState: .Normal)
        
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        btn.sizeToFit()
        return btn
    }()
    
    
    
}
